<?php
namespace app\admin\controller;
use app\admin\controller\Com;
use think\Db;
use think\File;
use app\admin\model\Upload;
use \think\Request;

class User extends Com
{
    public function index()
    {
        $resule=Db::name('user')->select();
        $this->assign('data',$resule);
        return view('user/index');
    }

    public function info()
    {
        $request=Request::instance();
        $id=$request->only(['id']);
        $resule=Db::name('user')->where('id',$id['id'])->select();
        // dump($resule);
        $this->assign('data',$resule[0]);
        return view('info');
    }

    public function edit()
    {
        $request=Request::instance();
        $id=$request->only(['id']);
        $resule=Db::name('user')->where('id',$id['id'])->select();
        // dump($resule);
        $this->assign('data',$resule[0]);
        return view('edit');
    }
    //修改用户信息程序
    public function editac()
    {
        dump(input('post.'));
        $info=input('post.');
        dump($info['birthday']);
        $birth=strtotime($info['birthday'].' 0:0:0');
        dump($birth);
        //上传文件处理
        // 获取表单提交过来的文件
        $fileinfo = $_FILES['img'];
        // dump($fileinfo);
        // exit();
        if ($fileinfo['error'] == 4) {
            //如果没有头像图片，直接修改基本的用户信息
            $resule=Db::name('user')
            ->where('id',$info['id'])
            ->update([
                'username' =>$info['username'],
                'user_number' =>$info['user_number'],
                'phone' =>$info['phone'],
                'sex' =>$info['sex'],
                'hobby' =>$info['hobby'],
                'birth' =>$birth,
                'constellation' =>$info['constellation'],
                'living_place' =>$info['living_place'],
                'hometown' =>$info['hometown'],
                'job' =>$info['job'],
                'is_marry' =>$info['is_marry']
            ]);
            // 写入数据库
            if ($resule) {
              return $this->success('无文件上传修改成功');
            } else {
                echo $resule->getlastsql();
                exit();
              return $this->error('无文件上传修改失败');
            }
        } else{
            //如果上传图片，需要修改图片的信息
            //查询出当前商品的老图信息
            $old_img=Db::name('user')->where('id',$info['id'])->value('img_src');
            //执行上传的操作
            //上传图片，得到图片的路径
            //设置上传的目录
            $path = 'static/upload/';
            $upload=new Upload;
            $result=$upload->upload($fileinfo, $path);
            if ($result['error']) {
                $new_img = $result['mess'];
                //有图片修改数据
                $resule=Db::name('user')
                ->where('id',$info['id'])
                ->update([
                    'username' =>$info['username'],
                    'user_number' =>$info['user_number'],
                    'phone' =>$info['phone'],
                    'sex' =>$info['sex'],
                    'hobby' =>$info['hobby'],
                    'birth' =>$birth,
                    'constellation' =>$info['constellation'],
                    'living_place' =>$info['living_place'],
                    'hometown' =>$info['hometown'],
                    'job' =>$info['job'],
                    'is_marry' =>$info['is_marry'],
                    'img_src' =>$new_img
                ]);
                // 写入数据库
                if($resule){
                    return $this->success('有文件上传修改成功');
                } else {
                    @unlink($path.$new_img);
                    return $this->error('有文件上传修改失败');
                }
            } else {
                //图片上传失败，执行返回修改页面
                // echo $resule->getlastsql();
                // exit();
                return $this->error('有文件上传修改失败1');
            }
        }

    }

}


